package com.cloudfun.campusshare.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cloudfun.campusshare.common.model.dto.FeedbackDTO;
import com.cloudfun.campusshare.common.model.dto.FeedbackSearchDTO;
import com.cloudfun.campusshare.common.model.vo.FeedbackExportVO;
import com.cloudfun.campusshare.common.model.vo.FeedbackListVO;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.entity.FeedbackEntity;
import com.cloudfun.campusshare.entity.UserEntity;
import com.cloudfun.campusshare.repo.FeedbackRepo;
import com.cloudfun.campusshare.repo.UserEntityRepo;
import com.cloudfun.campusshare.service.CreatorPersistenceService;
import com.cloudfun.campusshare.service.FeedbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by CodeGenerator on 2020/05/27
 */
@Slf4j
@Service
public class FeedbackServiceImpl extends CreatorPersistenceService implements FeedbackService {

    @Autowired
    private FeedbackRepo feedbackRepo;

    @Autowired
    private UserEntityRepo userEntityRepo;

    @Override
    protected Class<?> getEntityClass() {
        return FeedbackEntity.class;
    }

    @Override
    public void create(FeedbackDTO feedbackDTO) {
        FeedbackEntity feedbackEntity = new FeedbackEntity();
        BeanUtil.copyProperties(feedbackDTO, feedbackEntity);
        feedbackRepo.save(feedbackEntity);
    }

    @Override
    public void modify(FeedbackDTO feedbackDTO) {

    }

    @Override
    public PageResult<FeedbackListVO> search(Pageable pageable, FeedbackSearchDTO searchDTO) {
        Specification<FeedbackEntity> spec = this.spec(searchDTO);
        Page<FeedbackEntity> page = feedbackRepo.findAll(spec, pageable);
        List<FeedbackListVO> content = page.getContent().stream()
                .map(entity -> {
                    FeedbackListVO listVO = new FeedbackListVO();
                    BeanUtil.copyProperties(entity, listVO);
                    UserEntity creator = entity.getCreator();
                    if (null != creator) {
                        listVO.setCreatorPhone(creator.getPhone());
                    }
                    return listVO;
                }).collect(Collectors.toList());
        return new PageResult<>(page.getTotalElements(), content);
    }

    private Specification<FeedbackEntity> spec(FeedbackSearchDTO searchDTO) {
        return (root, query, cb) -> {
            List<Predicate> list = new ArrayList<>();
            if (null != searchDTO.getSource()) {
                list.add(cb.equal(root.get("source"), searchDTO.getSource()));
            }
            return cb.and(list.toArray(new Predicate[0]));
        };
    }

    @Override
    public void delete(String id) {
        feedbackRepo.deleteById(id);
    }

    @Override
    public List<FeedbackExportVO> export(List<String> ids, FeedbackSearchDTO searchDTO) {
        List<FeedbackEntity> loanEntities;
        if (!CollectionUtils.isEmpty(ids)) {
            loanEntities = feedbackRepo.findAllByIdInOrderByCreateTimeDesc(ids);
        } else if (null != searchDTO) {
            Specification<FeedbackEntity> specification = this.spec(searchDTO);
            loanEntities = feedbackRepo.findAll(specification, Sort.by(Sort.Direction.DESC, "createTime"));
        } else {
            loanEntities = feedbackRepo.findAll();
        }
        return this.toExportVO(loanEntities);
    }

    private List<FeedbackExportVO> toExportVO(List<FeedbackEntity> loanEntities) {
        return loanEntities.stream()
                .map(entity -> {
                    FeedbackExportVO exportVO = new FeedbackExportVO();
                    exportVO.setId(entity.getId());
                    UserEntity creator = entity.getCreator();
                    if (null != creator) {
                        exportVO.setCreatorPhone(creator.getPhone());
                    }
                    if (null != entity.getSource()) {
                        exportVO.setSource(entity.getSource().getDesc());
                    }
                    exportVO.setContent(entity.getContent());
                    exportVO.setCreateTime(new Date(Long.parseLong(entity.getCreateTime())));
                    return exportVO;
                }).collect(Collectors.toList());
    }
}